bshWX.controller('homepageCtrl',function($scope){
	
	$('.homepage_box .share').click(function(){
		$('.homepage_box .homepage_box .hiddenBox').css('display','block');
		setTimeout(function(){
			$('.homepage_box .hiddenBox').css('display','none');
		},1500)
	});

	$('.homepage_box .collection').click(function(){
		$('.homepage_box .hiddenBox').css('display','block');
		setTimeout(function(){
			$('.homepage_box .hiddenBox').css('display','none');
		},1500)
	});

	$('.homepage_box .payByerweima').click(function(){
		$('.homepage_box .hiddenBox').css('display','block');
		setTimeout(function(){
			$('.homepage_box .hiddenBox').css('display','none');
		},1500)
	});

	$.ajax({
		type : 'post',
		url : '/api/v1/merchant/H5MerchantHotList',
		dataType : 'json',
		data : {
			paramStr: '{}'
		},
		timeout : 10000,
		success : function (data) {
			if(data.code == 200){
				var obj = JSON.parse(data.data);
				var hotList = obj.hot_list;
				var $ul = $('.homepage_box .hotMerchants ul');
				for(var i = 0; i < hotList.length; i++) {
					$li = $('<li merchantId='+hotList[i]["id"]+'></li>').appendTo($ul);
					$('<img>').attr('src',hotList[i].imgCover).appendTo($li);
					$('<p></p>').addClass('p1').text(hotList[i].merchantName).appendTo($li);
					$('<p></p>').addClass('p2').text(hotList[i].traderDesc).appendTo($li);
				}

				$('.homepage_box .hotMerchants ul li').click(function(){
					sessionStorage.merchantId = $(this).attr('merchantId');
					sessionStorage.scrollTopValue = $(document).scrollTop();
					$('.homepage_box').css('display','none');
					$('.merchantDetailBox').css('display','block');
					$('.merchantDetailBox .hiddenBox').hide();
					getDetail(sessionStorage.merchantId);
					// window.location.href = './views/merchantsDetail.html';
				});
			}
		},
		error : function (xhr,type,errorThrown) {
			// alert('未知错误');
		}
	});

	$.ajax({
		type : 'post',
		url : '/api/v1/common/getIndexImages',
		dataType : 'json',
		timeout : 10000,
		success : function (data) {
			if(data.code == 200){
				var obj = JSON.parse(data.data);
				var indexImagesList = obj.indexImages;
				var boxHtml =" ";
				$.each(indexImagesList, function(i, item) {
					boxHtml += " <div class='swiper-slide'> <img src='" + item.img + "' style='width:100%;'/></div>";
				});
				$("#swiper-wrapper").html(boxHtml);
				var mySwiper = new Swiper('.swiper-container',{
		            pagination: '.pagination',
		            paginationClickable: true,
		            moveStartThreshold: 100,
		            observer:  true,//修改swiper自己或子元素时，自动初始化swiper
		            observeParents: true,//修改swiper的父元素时，自动初始化swiper
		            autoplay : 3000,
		            loop : true,
		            speed : 500
				});
				var $width = $('.pagination').width();
				$('.pagination').css('margin-left',-$width/2);
			}
		},
		error : function (xhr,type,errorThrown) {
			// alert('未知错误');
		}
	});

	function getDetail(id){
		$.ajax({
			type : 'post',
			url : '/api/v1/merchant/H5MerchantDetail',
			dataType : 'json',
			data : {
				paramStr: "{'merchant_id':'" + id + "'}"
			},
			timeout : 10000,
			success : function (data) {
				if(data.code == 200){
					var obj = JSON.parse(data.data);
					var shopDetail = obj.shop_detail;
					$('.merchantDetailBox .top img').attr('src',shopDetail.imgCover);
					$('.merchantDetailBox .p1').text('商户名：' + shopDetail.merchantName);
					$('.merchantDetailBox .p2').text('联系人：' + shopDetail.linkman);
					$('.merchantDetailBox .address').text('地址：' + shopDetail.address);
					$('.merchantDetailBox .telNum').text('电话：' + shopDetail.mobile);
					$('.merchantDetailBox .describe').text('商户描述：' + shopDetail.traderDesc);
					$('.merchantsDetail_title p').text(shopDetail.merchantName);

					$('.merchantDetailBox .merImgCover').attr('src',shopDetail.imgCover);
					if(!shopDetail.showImg1){
						$('.merchantDetailBox .merShowImg1').hide();
					}else{
						$('.merchantDetailBox .merShowImg1').attr('src',shopDetail.showImg1);
					}

					if(!shopDetail.showImg2){
						$('.merchantDetailBox .merShowImg2').hide();
					}else{
						$('.merchantDetailBox .merShowImg2').attr('src',shopDetail.showImg2);
					}

					$('.merchantDetailBox .merPicList img').bind('click',function(){
						sessionStorage.src = $(this).attr('src');
						$('.merchantDetailBox .hiddenBox').show();
						$('.merchantDetailBox .hiddenBox').html('');
						$('<img/>').attr('src',sessionStorage.src).appendTo($('.merchantDetailBox .hiddenBox'));
						var $width = $('.merchantDetailBox .hiddenBox img').width();
						var $height = $('.merchantDetailBox .hiddenBox img').height();
						$('.merchantDetailBox .hiddenBox img').css('width',10 + 'rem').css('height',10/($width/$height) + 'rem');
						$('.merchantDetailBox .hiddenBox').bind('click',function(){
							$('.merchantDetailBox .hiddenBox').hide();
						});
					});
				}
			},
			error : function (xhr,type,errorThrown){

			}
		});
	}
});





bshWX.controller('messageCtrl',function($scope){
	sendAjax(1);

	function remind(message){
		$('#remindBox').css('display','block').text(message);
		setTimeout(function(){
			$('#remindBox').css('display','none');
		},1500)
	}

	function sendAjax (page) {
		$.ajax({
			type : "POST",
			url : "/api/v1/message/list",
			data : {
				paramStr: "{'pageIndex':'" + page + "','pageSize':'" + 10 + "'}"
			},
			dataType : 'json',
			timeout : 10000,
			success : function (data) {
				if(data.code == 200){
					var obj = JSON.parse(data.data);
					var msgList = obj.msgList;
					if(msgList.length != 0){
						var $ul = $('#content ul');
						for(var i = 0; i < msgList.length; i++){
							var $li = $('<li dataid='+msgList[i]["id"]+'>'+'</li>').appendTo($ul);
							$('<p></p>').addClass('p1').text(msgList[i].create_time).appendTo($li);
							$('<p></p>').addClass('p2').text(msgList[i].title).appendTo($li);
						}	
					}else{
						remind('无更多消息');
					}

					$(document).on('click','#content ul li',function(){
						sessionStorage.dataid = $(this).attr('dataid');
						window.location.href = './views/messageDetails.html';
					});
				}
			},
			error : function (xhr,type,errorThrown){
				// alert('未知错误');
			}
		});
	}

	var abc = 1;
		
	refresher.init({
		id:"content",
		pullUpAction:Load
	});

	function Load(){
		abc++;
		sendAjax(abc);
		content.refresh();
	}
});


bshWX.controller('personalCtrl',function () {
	$.ajax({
		type : "POST",
		url : "/api/v1/user/info",
		data : {
			paramStr: "{'userName':'" + sessionStorage.userName + "','deviceType':'h5','token':'" + sessionStorage.token + "'}"
		},
		dataType : 'json',
		timeout : 10000,
		success : function (data) {
			if(data.code == 200){
				var obj = JSON.parse(data.data);
				// console.log(obj);
				sessionStorage.level = obj.level;
				sessionStorage.partnerLevel = obj.partnerLevel ? obj.partnerLevel:'';
				sessionStorage.isMerchant = obj.isMerchant;
				// sessionStorage.cloudShares = obj.cloudShares;
				var headImg = sessionStorage.headImg ? sessionStorage.headImg : './image/mylogo.png';

				$('#personal_username').text(sessionStorage.userName);
				$('#personal_level').text(sessionStorage.level + ' ' + sessionStorage.partnerLevel);
				$('.merCurrency_num').text(obj.cloudMoney);
				$('.cash_num').text(obj.money);
				$('.cashBack_num').text(obj.yestdayEarn);
				$('.headImg').attr('src',headImg);
				
				$('.exchange').click(function(){
					$('.hiddenBox').css('display','block').text('此功能尚未开发');
					setTimeout(function(){
						$('.hiddenBox').css('display','none');
					},1500);
				});

				$('.buy').click(function(){
					if(obj.isMerchant == -1){
						$('.hiddenBox').css('display','block').text('只允许商家购买');
						setTimeout(function(){
							$('.hiddenBox').css('display','none');
						},1500);
					}else if(obj.isMerchant == 1){
						sessionStorage.buyMobile = obj.mobile;
						window.location.href = 'views/buyMerCurrency.html'
					}
				});

				$('.b3').click(function(){
					$('.hiddenBox').css('display','block').text('请在APP中使用该功能');
					setTimeout(function(){
						$('.hiddenBox').css('display','none');
					},1500);
					// if($('#personal_level').text() == '金钻会员' && sessionStorage.isMerchant != '1'){
					// 	window.location.href = 'views/joinMerchants.html'
					// }else if($('#personal_level').text() == '金钻会员' && sessionStorage.isMerchant == '1'){
					// 	$('.remindBox').css('display','block');
					// 	$('.remindBox .innerBox .remind').text('已成为商户');
					// 	$('.innerBox_bottom').click(function(){
					// 		$('.remindBox').css('display','none');
					// 	});
					// }else{
					// 	$('.remindBox').css('display','block');
					// 	$('.remindBox .innerBox .remind').text('只有金钻会员才能成为商户');
					// 	$('.innerBox_bottom').click(function(){
					// 		$('.remindBox').css('display','none');
					// 	});
					// }
				});

				$('.b4').click(function(){
					if(sessionStorage.level != '金钻会员'){
						$('.remindBox').css('display','block');
						$('.remindBox .innerBox .remind').text('只有金钻会员才能成为合伙人');
						$('.innerBox_bottom').click(function(){
							$('.remindBox').css('display','none');
						});
					}else if(sessionStorage.level == '金钻会员' && obj.isPartner == 0 && obj.isApplyPartner == false){
						window.location.href = 'views/partner.html';
					}else if(sessionStorage.level == '金钻会员' && obj.isPartner == 1 && obj.isApplyPartner == true){
						// alert('已经是合伙人啦~');
						window.location.href = 'views/upPartner.html';
					}else if(sessionStorage.level == '金钻会员' && obj.isPartner == 0 && obj.isApplyPartner == true ){
						window.location.href = 'views/partnerDetails.html';
					}
				});

				$('.b2').click(function(){
					$('.hiddenBox').css('display','block').text('请在APP中使用该功能');
					setTimeout(function(){
						$('.hiddenBox').css('display','none');
					},1500)
				});
			}
		},
		error : function (xhr,type,errorThrown){
			// alert('未知错误');
		}
	});
});